package com.kevin.data.batch.controller;

import com.kevin.data.batch.configuration.enums.ComponentEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Tag(name = "任务管理")
@RequestMapping("/batch")
public class DataBatchController {

    @Resource(name = "asyncJob")
    private Job asyncJob;

    @Resource(name = "csvJob")
    private Job csvJob;

    @Resource(name = "dbJob")
    private Job dbJob;

    @Resource(name = "flowJob")
    private Job flowJob;

    @Resource(name = "jsonJob")
    private Job jsonJob;

    @Resource(name = "nestedJob")
    private Job nestedJob;

    @Resource(name = "readerWriterJob")
    private Job readerWriterJob;
    @Resource(name = "simpleJob")
    private Job simpleJob;

    @Resource
    private JobLauncher jobLauncher;

    @Operation(summary = "任务处理", description = "任务处理")
    @PostMapping(value = "/job")
    public String job(@RequestParam ComponentEnum jobEnum) {
        JobParameters jobParameters = new JobParametersBuilder()
                .addLong("timestamp", System.currentTimeMillis()).toJobParameters();
        Job job = switch (jobEnum) {
            case ASYNC_BATCH -> asyncJob;
            case CSV_BATCH -> csvJob;
            case DB_BATCH -> dbJob;
            case FLOW_BATCH -> flowJob;
            case JSON_BATCH -> jsonJob;
            case NESTED_BATCH -> nestedJob;
            case READER_WRITER_BATCH -> readerWriterJob;
            case SIMPLE_BATCH -> simpleJob;
        };
        if (null != job) {
            new Thread(() -> {
                try {
                    jobLauncher.run(job, jobParameters);
                } catch (Exception ignored) {

                }
            }).start();
        }
        return "success";
    }

}
